package pl.grabar.clinic.core.domain.person;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;

import pl.grabar.clinic.core.domain.enums.UserType;
import pl.grabar.clinic.core.domain.person.data.Address;
import pl.grabar.clinic.core.domain.person.data.Permission;

/**
 * Klasa odwzorowuje uzytkownika systemu-pacjenta
 * 
 * @author db
 */

@Entity
@Inheritance
@Table(name = "USER")
public class User implements Serializable {

	private static final long serialVersionUID = -6498741242164804113L;

	@Id
	@GeneratedValue
	@JsonIgnore
	private long id;

	@Column(name = "LOGIN", unique = true, nullable = false)
	@JsonIgnore
	private String login;

	@Column(name = "PASSWORD")
	@JsonIgnore
	private String password;

	@Column(name = "FIRST_NAME")
	private String firstName;
	
	@Column(name = "LAST_NAME")
	private String lastName;

	@Column(name="PHONE_NUMBER")
	@JsonIgnore
	private String phoneNumber;

	@Column(name="EMAIL")
	@JsonIgnore
	private String email;
	
	@Column(name = "USER_TYPE")
	@Enumerated(EnumType.STRING)
	@JsonIgnore
	private UserType userType;

	/**
	 * Identyfikator pacjenta w bazie. Potrzebny do podpięcia konta pacjenta do
	 * konta w systemie.
	 */
	@Column(name = "PATIENT_IDENTIFIER")
	@JsonIgnore
	private long patientIdentifier;

	@OneToOne(fetch=FetchType.EAGER, cascade = {CascadeType.ALL})
	@JoinColumn(name="ADDRESS_ID")
	@JsonIgnore
	private Address address;

	@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
	@JoinColumn(name = "USER_ID")
	@JsonIgnore
	private List<Permission> permissions;

	/* KONSTRUKTORY */

	public User() {
		permissions = new ArrayList<>();
	}

	public User(String login, String password) {
		this();
		this.login = login;
		this.password = password;
	}

	@JsonIgnore
	public String getFullName(){
		return this.firstName + " " + this.lastName;
	}
	
	/* AKCESORY */

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public UserType getUserType() {
		return userType;
	}

	public void setUserType(UserType userType) {
		this.userType = userType;
	}

	public List<Permission> getPermissions() {
		return permissions;
	}

	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}

	public long getPatientIdentifier() {
		return patientIdentifier;
	}

	public void setPatientIdentifier(long patientIdentifier) {
		this.patientIdentifier = patientIdentifier;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}
